/**
  ******************************************************************************
  * @file    si2c_sht3x2x.c 
  * @author  make
  * @version V1.0.0
  * @date    2020.10.09
  * @brief   src program body
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "si2c_comm.h"
#include "tskcfg.h"
#include "bsp.h"
#include "edct.h"

void sI2c_IOPowerReset(void)
{
  TaskHandle_t cur_tsk = NULL;
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  
  cur_tsk = xTaskGetCurrentTaskHandle();

#ifndef EDCT_JUST_PRESS  
  if((xSensorMain_Thd != NULL) && (xSensorMain_Thd != cur_tsk))
  {
    vTaskSuspend(xSensorMain_Thd);
  }
  
  if((xSgp30_Thd != NULL) && (xSgp30_Thd != cur_tsk))
  {
    vTaskSuspend(xSgp30_Thd);
  }
#endif
  
  if((xSdp810_Thd != NULL) && (xSdp810_Thd != cur_tsk))
  {
    vTaskSuspend(xSdp810_Thd);
  }

#ifndef EDCT_JUST_PRESS
  /* sht30 io */
  sht30_sda_l();
	sht30_scl_l();
	GPIO_InitStruct.Pin   = SHT30_SDA_PIN|SHT30_SCL_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	GPIO_InitStruct.Pull  = GPIO_PULLUP;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  
  /* sgp30 io */
  sgp30_sda_l();
	sgp30_scl_l();
	GPIO_InitStruct.Pin   = SGP30_SDA_PIN|SGP30_SCL_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	GPIO_InitStruct.Pull  = GPIO_PULLUP;
	HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
#endif
  
  /* sdp810 io */
  sdp810_sda_l();
	sdp810_scl_l();
	GPIO_InitStruct.Pin   = SDP810_SDA_PIN|SDP810_SCL_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	GPIO_InitStruct.Pull  = GPIO_PULLUP;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  
  /* power io */
  sensor_pwr_l();
  GPIO_InitStruct.Pin   = SENSOR_PWR_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	GPIO_InitStruct.Pull  = GPIO_PULLUP;
	HAL_GPIO_Init(SENSOR_PWR_GPIO_PORT, &GPIO_InitStruct);
  
  vTaskDelay(1000);
  sensor_pwr_h();
	vTaskDelay(1000);

#ifndef EDCT_JUST_PRESS
  if(Edct_RunPara.tvoc_en == 1)
	{
    Sgp30_ReInit();
	}

  if(Edct_RunPara.sdp810_en == 1)
#endif
	{
    Sdp810_ReInit();
  }

#ifndef EDCT_JUST_PRESS
  vTaskResume(xSensorMain_Thd);
  vTaskResume(xSgp30_Thd);
#endif
  
  vTaskResume(xSdp810_Thd);
}

uint8_t sensor_crc8(const uint8_t* value, uint8_t init)
{
	uint8_t  remainder;
	uint8_t  i, j;
	
	remainder = init;
	
	for(j = 0; j < 2; j++)
	{
		remainder ^= value[j];
		
		for(i = 0; i < 8; i++)
		{
			if(remainder & 0x80)
			{
				remainder = (remainder << 1)^0x31u;
			}
			else
			{
				remainder = (remainder << 1);
			}
		}
	}
	
	return remainder;
}
